package vn.aptech.scrumgroup.model;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;
import demo.axitdn.physic.Unit;

public class ScrumBoard extends AbstractBaseModel{

	public Point tlPoint;
	public String[] colNames;
	public int[] headerWidth;//width by number of cells
	public float cellSize;
	private int widthByCell;
	public int heightByCell;
	private float boardWidth;
	private float boardHeight;
	private float headerHeight;
	private Point[] colPoints;
	private Rect bound;
	private ArrayList<ItemModel> itemList;
	private int[][] grids;
	public boolean isBrilliant;
	int featureColor;
	
	public ScrumBoard(Point tlPoint, String[] colNames, float cellSize, int[] headerWidth, int heightByCell, int backColor, int foreColor, int featureColor) {
		// TODO Auto-generated constructor stub
		super(backColor, foreColor);
		this.tlPoint = tlPoint;
		this.colNames = colNames;
		this.headerWidth = headerWidth;
		this.cellSize = cellSize;
		for (int cellNo : headerWidth)
			this.widthByCell +=cellNo;
		this.heightByCell = heightByCell - 1;//input contains header
		grids = new int[widthByCell][this.heightByCell];
		itemList = new ArrayList<ItemModel>();
		this.featureColor = featureColor;
		init();
	}//end ctor
	
	private void init() {
		// TODO Auto-generated method stub
		boardWidth = widthByCell*cellSize;
		headerHeight = cellSize*1.2f;
		boardHeight = (heightByCell)*cellSize+headerHeight;
	
		colPoints = new Point[colNames.length-1];
		for (int i = 0; i<headerWidth.length-1; i++){
			if (i==0)
				colPoints[i] = new Point((int)(tlPoint.x+headerWidth[i]*cellSize), tlPoint.y);
			else
				colPoints[i] = new Point((int)(headerWidth[i]*cellSize+colPoints[i-1].x), tlPoint.y);
		}//end for
	    bound = new Rect();
	}//end init

	public void disableFeaturesColumn(boolean enable) {
		if (enable){
			//cannot move to feature column
		    for (int i = 0; i<headerWidth[0]; i++){
		    	for (int j = 0; j< heightByCell; j++){
		    		grids[i][j] = 1;
		    	}//end for
		    }//end for
		}//end if
	}//end method

	@Override
	public boolean isPointOf(float x, float y) {
		// TODO Auto-generated method stub
		float x1 = this.tlPoint.x;
		float y1 = this.tlPoint.y;
		float x2 = x1 + boardWidth;
		float y2 = y1 + boardHeight;
		if (x<x2&&x>x1&&y>y1&&y<y2){
			return true;
		}//end if
		return false;
	}

	@Override
	public void changeColor(int color) {
		// TODO Auto-generated method stub
		
	}
	
	public boolean putItem(ItemModel item, Point position){
		if (grids[position.x][position.y]==0){
			item.boardLeft = tlPoint.x;
			item.boardTop = tlPoint.y+headerHeight;
			item.cellSize = cellSize;
			item.left = position.x;
			item.top = position.y;
			grids[position.x][position.y]=1;
			if (!itemList.contains(item))
				itemList.add(item);
			return true;
		}
		return false;
	}//end method
	
	public boolean unPutItem(Point position){
		if (grids[position.x][position.y]==1){
			grids[position.x][position.y] = 0;
			return true;
		}
		return false;
	}
	
	public boolean moveItemCoordinate(ItemModel item, float x, float y){
		if (x<tlPoint.x||x>tlPoint.x+boardWidth||y<tlPoint.y+headerHeight||y>tlPoint.y+boardHeight){
			return false;
		}
		int ix = (int)((x-tlPoint.x)/cellSize);
		int iy = (int)((y-tlPoint.y-headerHeight)/cellSize);
		Log.d("move to: ", "(,"+ix+","+iy+")"+"with cell: "+cellSize);
		if (item.left!=ix||item.top!=iy){
			unPutItem(new Point(item.left, item.top));
			putItem(item, new Point(ix, iy));
			Log.d("put", "item pos: "+item.left+","+item.top+", board height"+boardHeight);
		}
		
		return true;
	}//end method
	
	public boolean removeItem(Point position){
		return false;
	}

	@Override
	public void onDraw(Canvas c) {
		// TODO Auto-generated method stub
		//draw Border
		drawBorder(c);
		//draw Header
		drawColumns(c);
		//draw Items
		//drawItems(c);
	}//end draw

//	private void drawItems(Canvas c) {
//		// TODO Auto-generated method stub
//		for (ItemModel item : itemList){
//			item.onDraw(c);
//		}//end for
//	}

	private void drawBorder(Canvas c) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		ColorMatrix cm = new ColorMatrix();
		int brightness = 120;
		if (isBrilliant){ 
			cm.set(new float[]{1,0,0,0,brightness,
		    		0,1,0,0,brightness,
		    		0,0,1,0,brightness,
		    		0,0,0,1,0
		    		});
			paint.setColorFilter(new ColorMatrixColorFilter(cm));
		}else {
			paint.setColorFilter(null);
		}//end else
		
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(2*Unit.i);
		float oldStrokeWidth = paint.getStrokeWidth();
		paint.setStrokeWidth(oldStrokeWidth/2);
		Rect innerRect = new Rect(tlPoint.x, tlPoint.y, (int)(tlPoint.x+boardWidth), (int)(tlPoint.y+boardHeight));
		
		Rect outerRect = new Rect((int)(tlPoint.x-cellSize/8), (int)(tlPoint.y-cellSize/8), (int)(tlPoint.x+cellSize/8+boardWidth), (int)(tlPoint.y+cellSize/8+boardHeight));
		
		c.drawRect(innerRect, paint);
		paint.setStrokeWidth(oldStrokeWidth);
		c.drawRect(outerRect, paint);
		
		oldStrokeWidth = paint.getStrokeWidth();
		paint.setStrokeWidth(oldStrokeWidth/2);
		c.drawLine(innerRect.left, innerRect.top, outerRect.left, outerRect.top, paint);
		c.drawLine(innerRect.right, innerRect.bottom, outerRect.right, outerRect.bottom, paint);
		c.drawLine(innerRect.right, innerRect.top, outerRect.right, outerRect.top, paint);
		c.drawLine(innerRect.left, innerRect.bottom, outerRect.left, outerRect.bottom, paint);
		paint.setStrokeWidth(oldStrokeWidth);
		
		
		//draw feature column
		paint.setStrokeWidth(1);
		paint.setColor(featureColor);
		paint.setAlpha(150);
		Rect colRect = new Rect(tlPoint.x, tlPoint.y, (int)(tlPoint.x+headerWidth[0]*cellSize), (int)(tlPoint.y+boardHeight));
		paint.setStyle(Style.FILL);
		c.drawRect(colRect, paint);
		
		
	}//end method

	private void drawColumns(Canvas c) {
		// TODO Auto-generated method stub
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(2*Unit.i);
		paint.setColor(backColor);
		
		c.drawLine(tlPoint.x, tlPoint.y+headerHeight, tlPoint.x+boardWidth, tlPoint.y+headerHeight, paint);
		for (Point pt : colPoints){
			c.drawLine(pt.x, pt.y, pt.x, pt.y+boardHeight, paint);
		}//end for
		//draw text header
		
		paint.setStrokeWidth(0.8f);
		paint.setColor(foreColor);
		for (int i = 0; i<colNames.length; i++){
			paint.getTextBounds(colNames[i], 0, colNames[i].length(), bound);
			int textWidth = bound.width();
			int textHeight = bound.height();
			int x = 0, y = 0;
			if (i==0){
				x = tlPoint.x;
				y = tlPoint.y;
			}
			else{
				x = colPoints[i-1].x;
				y = colPoints[i-1].y;
			}//end else
			if (textWidth<headerWidth[i]*cellSize){
				x = x + (int)(headerWidth[i]*cellSize-textWidth)/2;
			}else{
				x = x - (int)(textWidth-headerWidth[i]*cellSize)/2;
			}
			y = y + tlPoint.y + (int)(headerHeight-textHeight)/2;
			c.drawText(colNames[i], x, y, paint);
		}//end for
	}//end method

	@Override
	public void onUpdate(double t) {
		// TODO Auto-generated method stub
		
	}

	public boolean isPointOfColumn(int x, int y, int i) {
		// TODO Auto-generated method stub
		int x1 = this.tlPoint.x;
		int x2 = (int)(x1 + (i+1)*(headerWidth[0])*cellSize);
		int y1 = this.tlPoint.y;
		int y2 = (int)(y1 + boardHeight);
		if (x>x1&&x<x2&&y>y1&&y<y2)
			return true;
		return false;
	}
	
}
